package main

import (
    "fmt"
)

// Order 订单接口
type Order interface {
    ProcessOrder() error
    CalculateTotal() float64
    GetOrderType() string
    ValidateOrder() bool
}

// OrderProcessor 订单处理器接口（抽象创建者）
type OrderProcessor interface {
    CreateOrder(orderData map[string]interface{}) Order
    ProcessOrderWorkflow(order Order) error
}

// RegularOrder 普通订单
type RegularOrder struct {
    ID       string
    Items    []string
    Quantity []int
    Price    []float64
    UserID   string
}

func (r *RegularOrder) ProcessOrder() error {
    fmt.Printf("处理普通订单 %s\n", r.ID)
    if !r.ValidateOrder() {
        return fmt.Errorf("订单验证失败")
    }
    fmt.Printf("订单总金额: $%.2f\n", r.CalculateTotal())
    return nil
}

func (r *RegularOrder) CalculateTotal() float64 {
    total := 0.0
    for i, price := range r.Price {
        total += price * float64(r.Quantity[i])
    }
    return total
}

func (r *RegularOrder) GetOrderType() string {
    return "普通订单"
}

func (r *RegularOrder) ValidateOrder() bool {
    return len(r.Items) > 0 && len(r.UserID) > 0
}

// VIPOrder VIP订单
type VIPOrder struct {
    ID           string
    Items        []string
    Quantity     []int
    Price        []float64
    UserID       string
    DiscountRate float64
}

func (v *VIPOrder) ProcessOrder() error {
    fmt.Printf("处理VIP订单 %s（享受%.0f%%折扣）\n", v.ID, (1-v.DiscountRate)*100)
    if !v.ValidateOrder() {
        return fmt.Errorf("VIP订单验证失败")
    }
    fmt.Printf("VIP订单总金额: $%.2f\n", v.CalculateTotal())
    return nil
}

func (v *VIPOrder) CalculateTotal() float64 {
    total := 0.0
    for i, price := range v.Price {
        total += price * float64(v.Quantity[i])
    }
    return total * (1 - v.DiscountRate)
}

func (v *VIPOrder) GetOrderType() string {
    return "VIP订单"
}

func (v *VIPOrder) ValidateOrder() bool {
    return len(v.Items) > 0 && len(v.UserID) > 0 && v.DiscountRate >= 0
}

// RegularOrderProcessor 普通订单处理器（具体创建者）
type RegularOrderProcessor struct{}

func (r *RegularOrderProcessor) CreateOrder(orderData map[string]interface{}) Order {
    return &RegularOrder{
        ID:       orderData["id"].(string),
        Items:    orderData["items"].([]string),
        Quantity: orderData["quantity"].([]int),
        Price:    orderData["price"].([]float64),
        UserID:   orderData["user_id"].(string),
    }
}

func (r *RegularOrderProcessor) ProcessOrderWorkflow(order Order) error {
    fmt.Println("=== 普通订单处理流程 ===")
    fmt.Println("1. 验证库存")
    fmt.Println("2. 计算价格")
    err := order.ProcessOrder()
    if err != nil {
        return err
    }
    fmt.Println("3. 生成发货单")
    fmt.Println("4. 发送确认邮件")
    return nil
}

// VIPOrderProcessor VIP订单处理器
type VIPOrderProcessor struct{}

func (v *VIPOrderProcessor) CreateOrder(orderData map[string]interface{}) Order {
    return &VIPOrder{
        ID:           orderData["id"].(string),
        Items:        orderData["items"].([]string),
        Quantity:     orderData["quantity"].([]int),
        Price:        orderData["price"].([]float64),
        UserID:       orderData["user_id"].(string),
        DiscountRate: orderData["discount_rate"].(float64),
    }
}

func (v *VIPOrderProcessor) ProcessOrderWorkflow(order Order) error {
    fmt.Println("=== VIP订单处理流程 ===")
    fmt.Println("1. VIP身份验证")
    fmt.Println("2. 优先库存分配")
    fmt.Println("3. 应用VIP折扣")
    err := order.ProcessOrder()
    if err != nil {
        return err
    }
    fmt.Println("4. 优先发货处理")
    fmt.Println("5. 发送VIP专属通知")
    return nil
}

// 使用演示
func main() {
    fmt.Println("=== 订单处理器工厂演示 ===")
    
    // 普通订单处理
    regularProcessor := &RegularOrderProcessor{}
    regularData := map[string]interface{}{
        "id":       "ORD001",
        "items":    []string{"笔记本电脑", "鼠标"},
        "quantity": []int{1, 2},
        "price":    []float64{999.99, 29.99},
        "user_id":  "USER123",
    }
    
    regularOrder := regularProcessor.CreateOrder(regularData)
    regularProcessor.ProcessOrderWorkflow(regularOrder)
    
    fmt.Println()
    
    // VIP订单处理
    vipProcessor := &VIPOrderProcessor{}
    vipData := map[string]interface{}{
        "id":            "VIP001",
        "items":         []string{"高端显卡", "机械键盘"},
        "quantity":      []int{1, 1},
        "price":         []float64{1299.99, 199.99},
        "user_id":       "VIP456",
        "discount_rate": 0.15,
    }
    
    vipOrder := vipProcessor.CreateOrder(vipData)
    vipProcessor.ProcessOrderWorkflow(vipOrder)
}